<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备编码" prop="fDeviceCode">
        <el-input v-model="queryParams.fDeviceCode" placeholder="请输入设备编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="添加日期">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:post:add']">新增设备</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:post:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:post:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:post:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange" style="width: 100%">
      <el-table-column label="序号" align="center" prop="fId" />
      <el-table-column label="设备编码" align="center" prop="fDeviceCode" />
      <el-table-column label="设备名称" align="center" prop="fDeviceName" />
      <el-table-column label="设备品牌" align="center" prop="fDeviceBrand" />
      <el-table-column label="设备型号" align="center" prop="fDeviceModel" />
      <el-table-column label="所属工厂" align="center" prop="bcmOrg.fOrgName" />
      <el-table-column label="设备位置" align="center" prop="fDeviceLocation" /> <!-- 新增的列 -->
      <el-table-column label="负责人" align="center" prop="sysUser.nickName" />
      <!-- <el-table-column label="温箱数量" align="center" prop="postSort" /> -->
      <el-table-column label="状态" align="center" prop="fForbidStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.fForbidStatus" />
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="fCreaterName" />
      <el-table-column label="创建日期" align="center" prop="fCreateDate" width="180">
      </el-table-column>
      <el-table-column label="最后修改人" align="center" prop="fModifierName"  width="100"/>
      <el-table-column label="最后修改日期" align="center" prop="fModifyDate" width="180">
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" min-width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row, 1)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="75%">
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="设备编码" prop="fDeviceCode">
              <el-input v-model="form.fDeviceCode" placeholder="请输入设备编码" :disabled="isEdit == 1" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备名称" prop="fDeviceName">
              <el-input v-model="form.fDeviceName" placeholder="请输入设备名称" :disabled="isEdit == 1" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="采购日期" prop="fPurchaseDate">
              <el-date-picker v-model="form.fPurchaseDate" type="date" placeholder="选择日期" :disabled="isEdit == 1">
              </el-date-picker>
              <!-- <el-date-picker v-model="form.fPurchaseDate" style="width: 190px" value-format="yyyy-MM-dd"
                type="date"></el-date-picker> -->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="入场日期" prop="fInDate">
              <el-date-picker v-model="form.fInDate" type="date" placeholder="选择日期" :disabled="isEdit == 1">
              </el-date-picker>
              <!-- <el-date-picker v-model="form.fInDate" style="width: 190px" value-format="yyyy-MM-dd"
                type="date"></el-date-picker> -->
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="供应商名称" prop="fSupplierName">
              <el-input v-model="form.fSupplierName" placeholder="请输入" :disabled="isEdit == 1" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备品牌" prop="fDeviceBrand">
              <el-input v-model="form.fDeviceBrand" placeholder="请输入" :disabled="isEdit == 1" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="设备型号" prop="fDeviceModel">
              <el-input v-model="form.fDeviceModel" placeholder="请输入" :disabled="isEdit == 1" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="存储地址" prop="fStorageAddress">
              <el-input v-model="form.fStorageAddress" placeholder="请输入" :disabled="isEdit == 1" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="厂家联系人" prop="fContacts">
              <el-input v-model="form.fContacts" placeholder="请输入" :disabled="isEdit == 1" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="厂家联系电话" prop="fContactsPhone">
              <el-input v-model="form.fContactsPhone" placeholder="请输入" :disabled="isEdit == 1" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="负责人" prop="fUserId">
              <el-select v-loadmore="userLoad" v-model="form.fUserId" placeholder="请选择" :disabled="isEdit == 1">
                <el-option v-for="(item, index) in userList" :key="index" :label="item.nickName"
                  :value="item.userId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="负责人手机" prop="fUserPhone">
              <el-input v-model="form.fUserPhone" placeholder="请输入" :disabled="isEdit == 1" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="负责人邮箱" prop="fUserMail">
              <el-input v-model="form.fUserMail" placeholder="请输入" :disabled="isEdit == 1" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所属工厂" prop="fOrgId">
              <el-select v-loadmore="factoryLoad" v-model="form.fOrgId" placeholder="请选择" :disabled="isEdit == 1">
                <el-option v-for="(item, index) in factoryList" :key="index" :label="item.fOrgName"
                  :value="item.fId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="接口地址" prop="fApiUrl">
              <el-input v-model="form.fApiUrl" placeholder="请输入" :disabled="isEdit == 1" />
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="是否启用" prop="fForbidStatus">
              <el-radio-group v-model="form.fForbidStatus" :disabled="isEdit == 1">
                <el-radio v-for="dict in fForbidStatus" :key="dict.value" :label="dict.value">{{ dict.label
                  }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-button size="mini" type="primary" @click="handleCheck">检查</el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="备注" prop="fRemark">
            <el-input v-model="form.fRemark" type="textarea" placeholder="请输入内容" :disabled="isEdit == 1" />
          </el-form-item><br>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="isEdit != 1">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看
    <el-dialog :title="title" :visible.sync="opens" append-to-body width="75%">
      <el-form ref="form" :model="form" label-width="150px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="设备编码">
              <span>{{ form.fDeviceCode }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备名称">
              <span>{{ form.fDeviceName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="采购日期">
              <span>{{ form.fPurchaseDate }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="入场日期">
              <span>{{ form.fInDate }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="供应商名称">
              <span>{{ form.fSupplierName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备品牌">
              <span>{{ form.fDeviceBrand }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="设备型号">
              <span>{{ form.fDeviceModel }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="存储地址">
              <span>{{ form.fStorageAddress }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="厂家联系人">
              <span>{{ form.fContacts }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="厂家联系电话">
              <span>{{ form.fContactsPhone }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="负责人">
              <span>{{ form.fUserId }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="负责人手机">
              <span>{{ form.fUserPhone }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="负责人邮箱">
              <span>{{ form.fUserMail }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所属工厂">
              <span>{{ form.fOrgId }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所属产线">
              <span>{{ form.fLineId }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="是否启用">
              <span>{{ form.fForbidStatus === 0 ? '启用' : '停用' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="接口地址">
              <span>{{ form.fApiUrl }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注">
          <span>{{ form.remark }}</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="opens = false">关闭</el-button>
      </div>
    </el-dialog> -->

  </div>
</template>

<script>
import { listDevice, delDevice, addDevice, updateDevice, getCheck } from "@/api/basicData/equipment";
import { listOrg } from "@/api/basicData/factory"
import { listUser } from "@/api/system/user"

export default {
  name: "Device",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      isEdit: 0,
      opens: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 日期范围
      dateRange: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fDeviceCode: undefined,
        fDeviceName: undefined,
        status: undefined
      },
      // 表单参数
      form: {
        fId: undefined,
        fDeviceCode: '',
        fDeviceName: '',
        fPurchaseDate: '',
        fInDate: '',
        fSupplierName: '',
        fDeviceBrand: '',
        fDeviceModel: '',
        fStorageAddress: '',
        fContacts: '',
        fContactsPhone: '',
        fUserId: '',
        fUserPhone: '',
        fUserMail: '',
        fOrgId: '',
        fLineId: '',
        fForbidStatus: 0,
        fApiUrl: '',
        remark: ''
      },
      // 表单校验
      rules: {
        fDeviceCode: [
          { required: true, message: "设备编码不能为空", trigger: "blur" }
        ],
        fDeviceName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        fUserId: [
          { required: true, message: "负责人不能为空", trigger: "blur" }
        ],
        fOrgId: [
          { required: true, message: "所属工厂不能为空", trigger: "blur" }
        ],
        fApiUrl: [
          { required: true, message: "接口地址不能为空", trigger: "blur" }
        ],
        fForbidStatus: [
          { required: true, message: "请选择是否启用不能选空", trigger: "change" }
        ],
      },
      factoryWhere: { pageNum: 1, pageSize: 20 },
      userWhere: { pageNum: 1, pageSize: 20 },
      factoryList: [],
      userList: [],
      fForbidStatus: [
        { value: 0, label: '启用' },
        { value: 1, label: '停用' }
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    addDateRange(params, dateRange) {
      params.fCreateDateStart = dateRange[0];
      params.fCreateDateEnd = dateRange[1];
      return params;
    },
    // 查询设备列表
    getList() {
      this.loading = true;
      listDevice(this.addDateRange(this.queryParams, this.dateRange))
        .then(response => {
          this.postList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch(error => {
          console.error('获取设备列表失败', error);
          this.loading = false;
        });
    },
    // 取消操作
    cancel() {
      this.open = false;
      this.resetForm();
    },
    // 表单重置
    resetForm() {
      this.form = {
        fId: undefined,
        fDeviceCode: '',
        fDeviceName: '',
        fPurchaseDate: '',
        fInDate: '',
        fSupplierName: '',
        fDeviceBrand: '',
        fDeviceModel: '',
        fStorageAddress: '',
        fContacts: '',
        fContactsPhone: '',
        fUserId: '',
        fUserPhone: '',
        fUserMail: '',
        fOrgId: '',
        fLineId: '',
        fForbidStatus: 0,
        fApiUrl: '',
        fRemark: ''
      };
      this.factoryWhere.pageNum = 1
      this.userWhere.pageNum = 1
      this.factoryList = []
      this.userList = []
    },
    // 搜索操作
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置搜索条件
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        fDeviceCode: undefined,
        fDeviceName: undefined,
        status: undefined
      };
      this.dateRange = [];
      this.resetForm();
      this.handleQuery();
    },
    // 多选框选中数据处理
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 新增设备操作
    handleAdd() {
      this.isEdit = 0
      this.resetForm();
      this.getFactory()
      this.getUser()
      this.open = true;
      this.title = "新增设备";
    },
    closeDialog() {
      this.factoryWhere.pageNum = 1
      this.userWhere.pageNum = 1
      this.factoryList = []
      this.userList = []
    },
    getFactory() {
      listOrg(this.factoryWhere).then(res => {
        this.factoryList = [...this.factoryList, ...res.rows]
      })
    },
    getUser() {
      listUser(this.userWhere).then(res => {
        this.userList = [...this.userList, ...res.rows]
      })
    },
    factoryLoad() {
      this.factoryWhere.pageNum += 1
      this.getFactory()
    },
    userLoad() {
      this.userWhere.pageNum += 1
      this.getUser()
    },
    // 修改设备操作
    handleUpdate(row, type) {
      this.isEdit = type
      this.getFactory()
      this.getUser()
      this.resetForm();
      Object.assign(this.form, row);
      this.open = true;
      if (type === 1) {
        this.open = true;
        this.title = "查看设备";
      } else {
        this.open = true;
        this.title = "修改设备";
      }
    },

    // 查看设备操作
    handleCheck() {
      getCheck({fApiUrl:this.form.fApiUrl}).then(response => {
        this.$modal.msgSuccess(response.msg);
      }).catch(error => {
        console.error('接口请求错误:', error);
      });
    },
    // 提交表单操作
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.fId) {
            updateDevice(this.form)
              .then(response => {
                this.$modal.msgSuccess('修改成功');
                this.open = false;
                this.getList();
              })
              .catch(error => {
                console.error('修改设备失败', error);
              });
          } else {
            addDevice(this.form)
              .then(response => {
                this.$modal.msgSuccess('新增成功');
                this.open = false;
                this.getList();
              })
              .catch(error => {
                console.error('新增设备失败', error);
              });
          }
        }
      });
    },
    // 删除设备操作
    handleDelete(row) {
      const deviceId = row.fId || this.ids;
      this.$modal.confirm('是否确认删除设备编号为"' + deviceId + '"的数据项？')
        .then(() => {
          delDevice(deviceId)
            .then(() => {
              this.getList();
              this.$modal.msgSuccess('删除成功');
            })
            .catch(error => {
              console.error('删除设备失败', error);
            });
        })
        .catch(() => { });
    },
    // 导出操作
    handleExport() {
      // 实现导出逻辑
    }
  }
};
</script>
